package com.gome.axwl.aoc.facade.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

/**
 * Created by wanglongyu on 2018/7/20.
 */
public class DownloadFileUtil {
    /***
     * 根据文件网络链接地址
     * 获取文件输入流
     * @param url
     * @return
     */
    public static InputStream getFile(String url){
        // 输入流
        InputStream in = null;
        // 文件输出流
        try{
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,5000); //设置连接超时为5秒
            HttpClient client = new DefaultHttpClient(httpParams); // 生成一个http客户端发送请求对象
            HttpGet httpget1 = new HttpGet(url); //对查询页面get
            HttpResponse httpResponse1 = client.execute(httpget1); // 发送请求并等待响应
            // 判断网络连接是否成功
            System.out.println("状态码："+httpResponse1.getStatusLine().getStatusCode());
            if (httpResponse1.getStatusLine().getStatusCode() != 200)
                System.out.println("网络错误异常！!!!");
            else
                System.out.println("网络连接成功!!!");
            httpget1.abort(); //关闭get
            HttpGet httpget2 = new HttpGet(url); //对下载链接get实现下载
            HttpResponse httpResponse2 = client.execute(httpget2);
            HttpEntity entity = httpResponse2.getEntity(); // 获取响应里面的内容
            in = entity.getContent(); // 得到服务气端发回的响应的内容（都在一个流里面）
        }catch(Exception e){
            e.printStackTrace();
        }
        return in;
    }

    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
}
